package leetcode;

import java.util.Stack;

public class LeetCode150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int result = 0;
        for (String str : tokens) {
            if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
                result = calculation(stack.pop(), stack.pop(), str);
            } else {
                result = Integer.parseInt(str);
            }
            stack.push(result);
        }
        return result;
    }

    private int calculation(int back, int front, String symbol) {
        switch (symbol) {
            case "+":
                return front + back;
            case "*":
                return front * back;
            case "-":
                return front - back;
            case "/":
                return front / back;
        }
        return -1;
    }
//------------------------------------------------------------------
}
